package com.example.service.aboutUser.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vo.request.CreatePermissionForm;
import com.example.vo.request.EditPermissionDesForm;
import com.example.vo.response.SystemPermissionDTO;
import com.example.mapper.aboutUser.PermissionMapper;
import com.example.pojo.aboutUser.SystemPermission;
import com.example.service.aboutUser.PermissionService;
import com.example.vo.Result;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, SystemPermission> implements PermissionService {

    /**
     * 获取系统所有权限
     *
     * @return 系统所有权限dto
     */
    @Override
    public Result getAllPermissions() {
        LambdaQueryWrapper<SystemPermission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SystemPermission::getPermissionState, true)
                .eq(SystemPermission::getIsDelete, false);

        List<SystemPermission> permissionsList = super.list(lambdaQueryWrapper);

        List<SystemPermissionDTO> systemPermissionDTOS = BeanUtil.copyToList(permissionsList, SystemPermissionDTO.class);

        return Result.success(systemPermissionDTOS);
    }

    /**
     * 修改权限描述
     *
     * @param editPermissionDesForm 包括权限id,权限描述
     * @return 是否修改成功
     */
    @Override
    public Result updatePermissionDescription(EditPermissionDesForm editPermissionDesForm) {
        LambdaUpdateWrapper<SystemPermission> systemPermissionLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        systemPermissionLambdaUpdateWrapper.eq(SystemPermission::getId, editPermissionDesForm.getPermissionId())
                .set(SystemPermission::getPermissionDescription, editPermissionDesForm.getPermissionDescription());

        super.update(systemPermissionLambdaUpdateWrapper);

        return Result.success();
    }

    /**
     * 新增权限
     *
     * @param createPermissionForm 新增权限表单
     * @return 是否新增成功
     */
    @Override
    public Result createPermission(CreatePermissionForm createPermissionForm) {
        SystemPermission systemPermission = BeanUtil.copyProperties(createPermissionForm, SystemPermission.class);

        super.save(systemPermission);

        return Result.success();
    }
}
